{% extends "admin/layout.html" %}

{% block content %}
<div class="card">
    <div class="card-header">
        <h4>任务管理</h4>
    </div>
    <div class="card-body">
        <form method="POST" action="{{ url_for('jira.bulk_assign_tasks') }}">
            <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">

            <div class="mb-3">
                <label class="form-label">选择项目</label>
                <select class="form-select project-select" onchange="filterTasks(this.value)">
                    <option value="">-- 所有项目 --</option>
                    {% for project in projects %}
                    <option value="{{ project.id }}">{{ project.name }}</option>
                    {% endfor %}
                </select>
            </div>

            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th><input type="checkbox" id="select-all"></th>
                            <th>任务名称</th>
                            <th>所属项目</th>
                            <th>所属阶段</th>
                            <th>当前负责人</th>
                            <th>状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for project in projects %}
                            {% for stage in project.stages %}
                                {% for task in stage.tasks %}
                                <tr class="task-row" data-project="{{ project.id }}">
                                    <td><input type="checkbox" name="task_ids" value="{{ task.id }}"></td>
                                    <td>{{ task.title }}</td>
                                    <td>{{ project.name }}</td>
                                    <td>{{ stage.name }}</td>
                                    <td>
                                        {% if task.assignee %}
                                            {{ task.assignee.name }}
                                        {% else %}
                                            <span class="text-danger">未分配</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ task.status }}</td>
                                </tr>
                                {% endfor %}
                            {% endfor %}
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <div class="row mt-3">
                <div class="col-md-6">
                    <label class="form-label">批量分配给</label>
                    <select name="assignee_id" class="form-select member-select" required>
                        <option value="">-- 选择成员 --</option>
                        {% for project in projects %}
                            {% for member in project.members %}
                            <option value="{{ member.member_id }}">{{ member.member.name }}</option>
                            {% endfor %}
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-6 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary">批量分配</button>
                </div>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 初始化成员选择器
    $('.member-select').select2({
        placeholder: "搜索并选择成员..."
    });

    // 全选/取消全选
    $('#select-all').change(function() {
        $('input[name="task_ids"]').prop('checked', this.checked);
    });

    // 项目选择器
    $('.project-select').select2();
});

function filterTasks(projectId) {
    if (!projectId) {
        $('.task-row').show();
    } else {
        $('.task-row').hide();
        $(`.task-row[data-project="${projectId}"]`).show();
    }
}
</script>
{% endblock %}